const jwt = require('jsonwebtoken');
const {Student, Teacher} = require('../models/index');
const {jwtSecret} = require('../config/setting')

const auth = async (req, res, next) => {
    // token from request header
    const token = String(req.body.authorization).split(' ').pop();
    // JWT token verification
    const {id} = jwt.verify(token, jwtSecret);
    // User in database
    const user = await Student.findOne({where: {stu_id: id}});

    req.user = user;

    console.log(user);

    next()
}

module.exports = auth